package com.imprenta.ejb;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.hibernate.util.HibernateUtil;
import com.imprenta.model.TClienteBase;

public class EjbTCliente {
	
	@SuppressWarnings("rawtypes")
	public static ArrayList<TClienteBase> getListTipoCliente(){
		ArrayList<TClienteBase> _tipoClientBase = new ArrayList<TClienteBase>();
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String qry = "select tipoclienteid, descripcion from Tipocliente";
			Query q = session.createQuery(qry);
			List l = q.list();
			
			for (int i = 0; i < l.size(); i++) {
				//Tipocliente tc = (Tipocliente) l.get(i);
				Object[] items = (Object[])l.get(i);
				TClienteBase tcb = new TClienteBase();
				
				tcb.setTipoclienteid(Integer.parseInt(items[0].toString()));
				tcb.setDescripcion(items[1].toString());
				
				_tipoClientBase.add(tcb);
			}
		} catch (HibernateException he) {
			he.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		return _tipoClientBase;
	}

	@SuppressWarnings("rawtypes")
	public static TClienteBase getListTipoClienteById(int id){
		TClienteBase t = new TClienteBase();
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		try {
			String qry = "select tipoclienteid, descripcion from Tipocliente where tipoclienteid = :id";
			Query q = session.createQuery(qry);
			q.setParameter("id", id);
			List l = q.list();
			
			for (int i = 0; i < l.size(); i++) {
				Object[] items = (Object[])l.get(i);
				
				t.setTipoclienteid(Integer.parseInt(items[0].toString()));
				t.setDescripcion(items[1].toString());
			}
		} catch (HibernateException he) {
			he.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		
		return t;
	}
}